#include "../inc/fs_db.h"
#include <stdio.h>
#include <stdlib.h>

int test_fs(void);

int init_db(void);
int main()
{

    // 创建数据库，并初始化用户
    if (init_db() == -1)
    {
        return -1;
    }

    printf("Hello!World!\n");

    return 0;
}

// main.c
int init_db(void)
{
    sqlite3 *db = fs_db_init();
    if (!db)
    {
        fprintf(stderr, "无法初始化数据库\n");
        return -1;
    }

    // 确保根目录存在
    int root_id = fs_get_directory_id(db, "/");
    if (root_id == -1)
    {
        fprintf(stderr, "根目录不存在\n");
        return -1;
    }

    // 用户配置
    struct UserConfig
    {
        const char *name;
        const char *email;
    } users[] = {
        {"user3", "user3@qq.com"},
        {"user1", "user1@qq.com"},
        {"user2", "user2@qq.com"},
        {"admin", "admin@qq.com"} // admin是特殊用户
    };

    // 创建用户（跳过已存在用户）
    for (int i = 0; i < sizeof(users) / sizeof(users[0]); i++)
    {
        const char *username = users[i].name;
        const char *email = users[i].email;

        if (fs_user_exists(db, username))
        {
            printf("用户 %s 已存在\n", username);
            continue;
        }

        // 创建用户
        int user_id = fs_create_user(db, username, email, "password123");
        if (user_id == -1)
        {
            fprintf(stderr, "创建用户 %s 失败\n", username);
            continue;
        }

        printf("创建用户 %s 成功，ID: %d\n", username, user_id);

        // 创建用户目录
        char user_path[MAX_PATH_LEN];
        snprintf(user_path, sizeof(user_path), "/%s", username);

        int dir_id = fs_create_directory(db, user_path, username, user_id);
        if (dir_id == -1)
        {
            fprintf(stderr, "创建用户目录 %s 失败\n", user_path);
        }
        else
        {
            printf("创建用户目录 %s 成功，ID: %d\n", user_path, dir_id);
        }
    }

    // 添加测试文件到user3
    int user3_id = fs_get_user_id(db, "user3");
    if (user3_id != -1)
    {
        // 获取user3目录ID
        int user3_dir_id = fs_get_directory_id(db, "/user3");
        if (user3_dir_id != -1)
        {
            // 添加文件
            int file_id = fs_add_file(
                db,
                "/user3/file3.txt", // 绝对路径
                "file3.txt",
                user3_id,
                0,
                false,
                user3_dir_id);

            if (file_id == -1)
            {
                fprintf(stderr, "添加文件失败\n");
            }
            else
            {
                printf("添加文件成功，ID: %d\n", file_id);
            }
        }
        else
        {
            fprintf(stderr, "找不到/user3目录\n");
        }
    }
    else
    {
        fprintf(stderr, "找不到user3用户\n");
    }

    fs_db_close(db);
    return 0;
}

int test_fs(void)
{
    printf("动态文件系统数据库接口测试\n");
    printf("=========================\n");

    // 初始化数据库
    sqlite3 *db = fs_db_init();
    if (!db)
    {
        fprintf(stderr, "无法初始化数据库\n");
        return 1;
    }

    // 创建用户
    int admin_id = fs_create_user(db, "admin", "admin@example.com", "admin_pass");
    if (admin_id > 0)
    {
        printf("创建用户 admin, ID: %d\n", admin_id);
    }

    int user1_id = fs_create_user(db, "user1", "user1@example.com", "user1_pass");
    if (user1_id > 0)
    {
        printf("创建用户 user1, ID: %d\n", user1_id);
    }

    // 创建目录
    int user1_dir_id = fs_create_directory(db, "user1", "user1", user1_id);
    if (user1_dir_id > 0)
    {
        printf("创建用户目录 user1, ID: %d\n", user1_dir_id);
    }

    int user1_subdir_id = fs_create_directory(db, "user1/documents", "documents", user1_id);
    if (user1_subdir_id > 0)
    {
        printf("创建子目录 documents, ID: %d\n", user1_subdir_id);
    }

    // 添加文件 (模拟)
    int file1_id = fs_add_file(db, "user1/file1.txt", "file1.txt", user1_id, 1024, false, user1_dir_id);
    if (file1_id > 0)
    {
        printf("添加文件 file1.txt, ID: %d\n", file1_id);
    }

    int file2_id = fs_add_file(db, "user1/documents/report.pdf", "report.pdf", user1_id, 2048, false, user1_subdir_id);
    if (file2_id > 0)
    {
        printf("添加文件 report.pdf, ID: %d\n", file2_id);
    }

    // 列出用户文件
    fs_list_user_files(db, user1_id);

    // 扫描用户目录 (更新数据库)
    printf("扫描用户目录 user1...\n");
    fs_scan_user_directory(db, "user1");

    // 再次列出用户文件
    fs_list_user_files(db, user1_id);

    // 删除文件 (模拟)
    printf("删除文件 user1/file1.txt...\n");
    fs_delete_file(db, "user1/file1.txt");

    // 最终列出用户文件
    fs_list_user_files(db, user1_id);

    // 关闭数据库
    fs_db_close(db);
}